import { Controller } from "../../mvc/core/Controller";
import { Dispatcher } from "../../mvc/core/Dispatcher";
import { EventName } from "../mvc/EventName";
import { RolePlayer } from "../scene/player/entity/RolePlayer";
import { PlayerEvent } from "../scene/events/PlayerEvent";
import { DataEvent } from "../events/DataEvent";
import { TaskTargetInfo } from "../model/TaskTargetInfo";
import { EntityType } from "../rules/EntityType";
import { GameController } from "../mvc/GameController";
import { MonsterPlayer } from "../scene/player/entity/MonsterPlayer";
//class GuideDirController
    
    export  class GuideDirController extends Controller
    {
       

        /* internal  */initListener(): void
        {
            Dispatcher.addEventListener(EventName.Scene_Update, this.onSceneUpdateHandler);
            Dispatcher.addEventListener(EventName.AI_NpcInDestance, this.onInNpcDistanceHandler);
            RolePlayer.instance.addEventListener(PlayerEvent.PLAYER_FIRE, this.onPlayerFireHandler);
            RolePlayer.instance.addEventListener(PlayerEvent.GIRD_WALK_END, this.onGridWorkEndHandler);
            return;
        }

        /* internal  */removeListener(): void
        {
            Dispatcher.removeEventListener(EventName.Scene_Update, this.onSceneUpdateHandler);
            Dispatcher.removeEventListener(EventName.AI_NpcInDestance, this.onInNpcDistanceHandler);
            RolePlayer.instance.removeEventListener(PlayerEvent.PLAYER_FIRE, this.onPlayerFireHandler);
            RolePlayer.instance.removeEventListener(PlayerEvent.GIRD_WALK_END, this.onGridWorkEndHandler);
            return;
        }

        /* internal  */checkDirGuide(arg1: Boolean=false): void
        {
            var loc1=0;
            if (cache.guideDir.inGuideDir) 
            {
                if (!arg1 || cache.guideDir.checkDisGuide()) 
                {
                    loc1 = cache.guideDir.guideDir;
                    RolePlayer.instance.updateGuideDir(loc1);
                }
                else 
                {
                    this.clearGuide();
                }
            }
            else 
            {
                this.endDirGuide();
            }
            return;
        }

        /* internal  */endDirGuide(): void
        {
            this.removeListener();
            cache.guideDir.endGuideDir();
            this.clearGuide();
            return;
        }

        /* internal  */clearGuide(): void
        {
            RolePlayer.instance.endGuideDir();
            return;
        }

        /* internal  */onGuideDirBeginHandler(arg1: DataEvent): void
        {
            this.initListener();
            cache.guideDir.beginGuide();
            cache.guideDir.updateTarget(arg1.data as TaskTargetInfo);
            this.checkDirGuide(true);
            return;
        }

        /* internal  */onGuideDirEndHandler(arg1: DataEvent): void
        {
            this.endDirGuide();
            return;
        }

        /* internal  */onSceneUpdateHandler(arg1: DataEvent): void
        {
            if (cache.guideDir.inGuideDir) 
            {
                this.checkDirGuide(true);
            }
            return;
        }

        /* internal  */onInNpcDistanceHandler(arg1: DataEvent): void
        {
            if (cache.guideDir.inGuideDir) 
            {
                if (cache.guide.dalayTarget && cache.guide.dalayTarget.targetType == EntityType.NPC && cache.dialog.npcInfo) 
                {
                    if (cache.dialog.npcInfo.tnpc.npcId == cache.guide.dalayTarget.id) 
                    {
                        this.endDirGuide();
                    }
                }
            }
            return;
        }

        /* internal  */onPlayerFireHandler(arg1: PlayerEvent): void
        {
            if (cache.guideDir.inGuideDir) 
            {
                if (cache.guide.dalayTarget && cache.guide.dalayTarget.targetType == EntityType.Boss) 
                {
                    if (GameController.scene.selectEntity && GameController.scene.selectEntity is MonsterPlayer) 
                    {
                        if ((GameController.scene.selectEntity as MonsterPlayer).bossCode == cache.guide.dalayTarget.id) 
                        {
                            this.endDirGuide();
                        }
                    }
                }
            }
            return;
        }

        /* internal  */onGridWorkEndHandler(arg1: PlayerEvent): void
        {
            if (cache.guideDir.inGuideDir) 
            {
                this.checkDirGuide();
            }
            return;
        }

        protected /* override */ initServer(): void
        {
            super.initServer();
            Dispatcher.addEventListener(EventName.GuideDirBegin, this.onGuideDirBeginHandler);
            Dispatcher.addEventListener(EventName.GuideDirEnd, this.onGuideDirEndHandler);
            return;
        }
    }
